package io.github.talelin.latticy.controller.zty;

import io.github.talelin.core.annotation.LoginRequired;
import io.github.talelin.latticy.common.LocalUser;
import io.github.talelin.latticy.dto.zty.ShopListDTO;
import io.github.talelin.latticy.model.UserDO;
import io.github.talelin.latticy.service.WarehouseInputListService;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.zty.InputDetailVO;
import io.github.talelin.latticy.vo.zty.InputListVO;
import io.github.talelin.latticy.vo.zty.PurchaseListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.List;

@RestController
@RequestMapping("/warehouseInput")
public class WarehouseInputController {


    @Autowired
    WarehouseInputListService warehouseInputListService;

    @PostMapping("/purchaseList")
    @LoginRequired
    @Transactional
    public CreatedVO createPurchaseList(@RequestBody ShopListDTO shopListDTO) {
        System.out.println(shopListDTO);
        UserDO localUser = LocalUser.getLocalUser();
        System.out.println(localUser);
        shopListDTO.getShopListDetails().forEach(System.out::println);
        warehouseInputListService.createPurchaseList(shopListDTO);
        return new CreatedVO(19);
    }


    @PutMapping("/inputList/{id}/{handlerId}")
    @LoginRequired
    @Transactional
    public CreatedVO createInputList(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id, @PathVariable(value = "handlerId") @Positive(message = "{handlerId.positive}") Integer handlerId) {
        warehouseInputListService.createInputList(id, handlerId);
        return new CreatedVO(19);
    }

    @PutMapping("/returnList/{id}/{handlerId}")
    @LoginRequired
    @Transactional
    public CreatedVO createReturnList(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id, @PathVariable(value = "handlerId") @Positive(message = "{handlerId.positive}") Integer handlerId) {
        warehouseInputListService.createReturnList(id, handlerId);
        return new CreatedVO(19);
    }

    @GetMapping("/purchase/list")
    @LoginRequired
    public List<PurchaseListVO> getPurchaseList(){
        List<PurchaseListVO> purchaseListVOs = warehouseInputListService.getPurchaseLists();
        return purchaseListVOs;
    }

    @GetMapping("/input/list")
    @LoginRequired
    public List<InputListVO> getinputList(){
        List<InputListVO> inputLists = warehouseInputListService.getInputLists();
        return inputLists;
    }

    @GetMapping("/purchase/details/{id}")
    @LoginRequired
    public List<InputDetailVO>  getPurchaseDetailById(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id){
        System.out.println(id);
        List<InputDetailVO> purchaseDetails = warehouseInputListService.getPurchaseDetailsByPurchaseListId(id);
        return purchaseDetails;
    }

    @GetMapping("/input/details/{id}")
    @LoginRequired
    public List<InputDetailVO>  getInputDetailById(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id){
        System.out.println(id);
        List<InputDetailVO> purchaseDetails = warehouseInputListService.getPurchaseDetailsByInputListId(id);
        return purchaseDetails;
    }
}
